package com.yao.club.controller;

import com.yao.club.entity.result.LoginResultEntity;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.request.ClubAuthRequestEntity;
import com.yao.club.service.UserService;
import com.yao.club.token.PassToken;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app/club_auth")
@Api(value = "校验接口", description = "该模块提供用户校验服务", tags = "用户校验模块")
public class ClubAuthController {

    @Autowired
    UserService userService;

    @PassToken
    @ApiOperation(value = "登陆-校验验证码/账号密码", notes = "登陆-校验验证码/账号密码", httpMethod = "POST")
    @PostMapping("/login")
    public ResponseEntity<LoginResultEntity> login(@RequestBody
                                                     @ApiParam(value = "", name = "entity", required = true)
                                         ClubAuthRequestEntity entity){

        if("sms".equals(entity.grantType)){
            return userService.login(entity);
        }else if ("password".equals(entity.grantType)){
            return userService.loginByPwd(entity);
        }
        return null;

    }
    @PassToken
    @ApiOperation(value = "忘记密码-校验验证码", notes = "忘记密码-校验验证码", httpMethod = "POST")
    @PostMapping("/resetPwd")
    public ResponseEntity<Boolean> resetPwd(@RequestBody
                                                   @ApiParam(value = "", name = "entity", required = true)
                                                   ClubAuthRequestEntity entity){
        return userService.resetPwd(entity);
    }

    @PassToken
    @ApiOperation(value = "绑定密码", notes = "绑定密码", httpMethod = "POST")
    @PostMapping("/resetNewPwd")
    public ResponseEntity<Boolean> resetNewPwd(@RequestBody
                                            @ApiParam(value = "", name = "entity", required = true)
                                            ClubAuthRequestEntity entity){
        return userService.resetNewPwd(entity);
    }

    @PassToken
    @ApiOperation(value = "极光一键登陆", notes = "极光一键登陆", httpMethod = "POST")
    @PostMapping("/oneQuickLogin")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "loginToken", value = "认证 SDK 获取到的 loginToken", required = true),
            @ApiImplicitParam(dataType = "String", name = "exID", value = "开发者自定义的 id，非必填", required = false)
    })
    public ResponseEntity<LoginResultEntity> oneQuickLogin(String loginToken,String exID) throws IOException {

        // 1. 构造请求URL和Headers
        String url = "https://api.verification.jpush.cn/v1/web/loginTokenVerify";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBasicAuth("appKey", "masterSecret");  // 自动Base64编码

        // 2. 构造请求体
        Map<String, String> body = new HashMap<>();
        body.put("loginToken", loginToken);
        body.put("exID", exID);

        // 3. 发送请求
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<Map<String, String>> entity = new HttpEntity<>(body, headers);
        org.springframework.http.ResponseEntity<Map> response = restTemplate.postForEntity(url, entity, Map.class);

        // 4. 处理响应
        if (response.getStatusCode() != HttpStatus.OK) {
            return ResponseUtils.failed("一键登陆失败");
        }
        Map<String, Object> result = response.getBody();
        if (!"8000".equals(result.get("code"))) {
            return ResponseUtils.failed("一键登陆失败");
        }
        Map<String, String> content = (Map) result.get("content");
        String phone = content.get("phone");
        return userService.oneQuickLogin(phone);
    }
}
